<?php
/**
* Django's templating engine (partial) PHP implementation.
* provides some template tags support
* @link http://djangoproject.com
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* tag node : if
* <code>{% if somevar %} somevar has a not null/false value : {{ somevar }}{% endif %}</code>
*/
class djtplTagNode_if extends djtplTagNode {

	/**
	* builds itself from tokens
	* @param djangoTplToken &$selfToken
	* @param array &$nextTokens djangoTplToken
	* @access public
	* @return bool
	*/
	function buildFromTokens(&$selfToken, &$nextTokens) {
		// importing def
		$this->def=$selfToken->tagdef;
		$this->buildChildNodesFromTokens($nextTokens);
		// getting if child nodes
		//$this->ifnodes=djangoTplParser::tokensToNodesUntilTag
		// getting else child nodes
		
		return true;
	}/**/

	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
	
	/**
	* renders
	* @param djangoTplContext
	* @access public
	* @return string
	*/
	function render(&$context) {
		// parsing def
		$words=explode(' ',$this->def);
		//echo wakdebug::vardump($words);
		$notFlag=false;
		$assoc=null;
		// $globalFlag=null;
		$values=array();
		// looping words
		foreach ($words as $word) {
			//echo $word;
			// var or control word ?
			if ($word=='not') {
				$notFlag=true;
			} else if ($word=='and' or $word=='or') {
				if (!$assoc)
					$assoc=$word;
			} else {
				// evaluating var
				//echo "getting $word";
				$value=djtplPArser::evalExpression($context,$word);
				//$value=$context->getPath($word);
				//echo wakdebug::vardump($value);
				$bool=$value?true:false;
				// is "not" flag active ?
				if ($notFlag)
					$bool=!$bool;
				// resetting "not" flag
				$notFlag=false;
				$values[]=$bool;
			}
		}
		//echo wakdebug::vardump($values);
		// global evaluating
		switch ($assoc) {
			case 'or':
				// only 1 value need to be true
				if (in_array(true,$values))
					return $this->renderChildNodesBeforeTag($context,'else');
				else
					return $this->renderChildNodesAfterTag($context,'else');
				break;
			case 'and':
			case null:
				// all values need to be true
				if (in_array(false,$values))
					return $this->renderChildNodesAfterTag($context,'else');
				else
					return $this->renderChildNodesBeforeTag($context,'else');
				break;
		}
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $tagname='if';
}/* end of djtplTagNode_if class */
?>
